package com.bookstore.services.Impl;

import com.bookstore.mappers.OrderItemMapper;
import com.bookstore.pojo.OrderItem;
import com.bookstore.services.OrderItemService;
import com.bookstore.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class OrderItemServiceImpl implements OrderItemService {
    @Override
    public List<OrderItem> getOrderItems(String orderId) {
        SqlSession sqlSession = null;
        List<OrderItem> orders = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            OrderItemMapper mapper = sqlSession.getMapper(OrderItemMapper.class);
            orders = mapper.getOrderItems(orderId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            SqlSessionUtil.close(sqlSession);
        }
        return orders;
    }

    @Override
    public void saveItem(List<OrderItem> orderItems) {
        SqlSession sqlSession = null;
        try {
            sqlSession = SqlSessionUtil.getSqlSession();
            OrderItemMapper mapper = sqlSession.getMapper(OrderItemMapper.class);
            for (OrderItem orderItem:orderItems) {
                mapper.saveOrderItem(orderItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            SqlSessionUtil.close(sqlSession);
        }
    }
}
